package org.crazyit.activiti_d4._1_GetResource;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;

import java.io.IOException;
import java.io.InputStream;

public class GetResource {

    public static void main(String[] args) {

        //1. 创建流程引擎
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();

        //2. 得到流程存储服务对象
        RepositoryService repositoryService = engine.getRepositoryService();

        //3. 部署一份txt 文件
        //注: 调用addClasspathResource() 方法时, 会以文件的路径作为该资源数据的名称
        Deployment dep = repositoryService.createDeployment().addClasspathResource("GetResource.txt").deploy();

        //4. 查询资源文件
        //注: 因此在查询时, 需要使用同样的路径作为资源名进行查询
        InputStream is = repositoryService.getResourceAsStream(dep.getId(), "GetResource.txt");

        try {

            //5. 读取输入流
            int count = is.available();
            byte[] contents = new byte[count];
            //InputStream 的read() 方法可以将输入流的内容复制到二进制数组中
            is.read(contents);
            String result = new String(contents);

            //6. 输出结果
            System.out.println(result);

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}
